package com.ouss.tabs;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.TagNode;

public class htmlHelper {
    TagNode rootNode;

    public htmlHelper(URL htmlPage) throws IOException
    {
        HtmlCleaner cleaner = new HtmlCleaner();
        rootNode = cleaner.clean(htmlPage);
    }

    List<TagNode> getLinksByClass(String CSSClassname)
    {
        List<TagNode> linkList = new ArrayList<TagNode>();

        TagNode linkElements[] = rootNode.getElementsByName("td", true);
        for (int i = 0; linkElements != null && i < linkElements.length; i++)
        {
        	// Log.v("htmlHelper", "element : "+linkElements[i].getText() + " | Class :"+linkElements[i].getAttributeByName("class"));
            String classType = linkElements[i].getAttributeByName("class");
            if (classType != null && classType.equals(CSSClassname))
            {
            	linkList.add(linkElements[i]);
            }
        }

        return linkList;
    }
    
    List<TagNode> getLinksByClass(String CSSClassname , String subDiv)
    {
        List<TagNode> linkList = new ArrayList<TagNode>();

        TagNode linkElements[] = rootNode.getElementsByName("td", true);
        for (int i = 0; linkElements != null && i < linkElements.length; i++)
        {
        	String classType = linkElements[i].getAttributeByName("class");
            if (classType != null && classType.equals(CSSClassname))
            {
            	TagNode linkElements2[] = linkElements[i].getElementsByName(subDiv, true);
            	for (int j = 0; linkElements2 != null && j < linkElements2.length; j++)
                {
            		linkList.add(linkElements[i]);
                }
            }
        	
        }

        return linkList;
    }
}
